package com.longmao.eduservice.controller;


import com.longmao.commonutils.R;
import com.longmao.eduservice.entity.EduChapter;
import com.longmao.eduservice.entity.vo.ChapterVo;
import com.longmao.eduservice.service.EduChapterService;
import org.omg.PortableServer.LIFESPAN_POLICY_ID;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author longmao
 * @since 2022-01-27
 */
@RestController
//@CrossOrigin
@RequestMapping("/eduservice/chapter")
public class EduChapterController {

    @Autowired
    private EduChapterService chapterService;

    /**
     * 课程大纲列表   根据课程id进行查询
     * @return
     */
    @GetMapping("/getChapterVideo/{courseId}")
    public R getChapterVideo(@PathVariable String courseId) {
        List<ChapterVo> list = chapterService.getChapterVideoByCourseId(courseId);
        return R.ok().data("allChapterVideo", list);
    }

    @PostMapping("/addChapter")
    public R addChapter(@RequestBody EduChapter eduChapter) {
        boolean result = chapterService.save(eduChapter);
        if(result) {
            return R.ok();
        }else {
            return R.error().message("添加失败");
        }
    }

    @PostMapping("/updateChapter")
    public R updateChapter(@RequestBody EduChapter eduChapter) {
        boolean result = chapterService.updateById(eduChapter);
        if(result) {
            return R.ok();
        }else {
            return R.error().message("修改失败");
        }
    }

    @GetMapping("/getChapterInfo/{chapterId}")
    public R getChapterInfoById(@PathVariable String chapterId) {
        EduChapter eduChapter = chapterService.getById(chapterId);
        return R.ok().data("eduChapter", eduChapter);
    }

    @DeleteMapping("/deleteChapter/{chapterId}")
    public R deleteChapter(@PathVariable String chapterId) {
        boolean result = chapterService.deleteChapterById(chapterId);
        if(result) {
            return R.ok();
        }else {
            return R.error().message("删除失败");
        }
    }
}

